package cn.softeem.tushu.Dao.Impl;

import cn.softeem.tushu.Dao.OrderDao;
import cn.softeem.tushu.po.*;
import cn.softeem.tushu.utils.BeanUtil;
import cn.softeem.tushu.utils.PageBean;
import cn.softeem.tushu.utils.TxQueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl implements OrderDao {
    private static final Integer pageCount=5;
    private TxQueryRunner tqr =new TxQueryRunner();
    @Override
    public void save(Order order) throws SQLException {
        tqr.update("insert into t_order values (?,?,?,?,?,?)",
                order.getOid(),order.getOrdertime(),order.getTotal(),
                order.getStatus(),order.getAddress(),order.getUser().getUid());
        int len = order.getOrderitems().size();
        Object[][] objects = new Object[len][];
        for (int i = 0; i <len ; i++) {
            Orderitem orderitem = order.getOrderitems().get(i);
            Book book = orderitem.getBook();
            objects[i]=new Object[]{orderitem.getOrderItemId(), orderitem.getQuantity(),orderitem.getSubtotal(),book.getBid(),
            book.getBname(),book.getCurrPrice(),book.getImage_b(),order.getOid()
            };
        }
        tqr.batch("insert into t_orderitem values(?,?,?,?,?,?,?,?)",objects);
    }

    @Override
    public PageBean<Order> getOrderList(String uid, int pageNow) throws SQLException {
        PageBean<Order>bean=new PageBean<>();
        bean.setPageNow(pageNow);
        //查询总记录数
        String sql="select count(*) from t_order where uid=?";
        bean.setTotalCount(tqr.query(sql,new ScalarHandler<Long>(),uid).intValue());
        sql="select * from t_order where uid=?  limit ?,?";
        List<Map<String, Object>> list = tqr.query(sql, new MapListHandler(), uid, (pageNow - 1) * pageCount, pageCount);
        List<Order> orderList = toBeanList(list);
        for (Order order : orderList) {
            loadOrderItem(order);
        }
        bean.setList(orderList);
        bean.setPageCount(pageCount);
        return bean;
    }

    @Override
    public Order desc(String oid) throws SQLException {
        Order order = tqr.query("select * from t_order where oid=?", new BeanHandler<>(Order.class), oid);
        loadOrderItem(order);
        return order;
    }

    @Override
    public void cancel(String oid) throws SQLException {
        tqr.update("update t_order set status = ? where oid=?",5,oid);
    }

    @Override
    public void confirm(String oid) throws SQLException {
        tqr.update("update t_order set status = ? where oid=?",4,oid);
    }
    public void pay(String oid) throws SQLException {
        tqr.update("update t_order set status = ? where oid=?",2,oid);
    }


    private Order toBean(Map<String,Object> map) {
        Order order = BeanUtil.toBeans(map,Order.class);
        User user=BeanUtil.toBeans(map,User.class);
        order.setUser(user);
        return  order;
    }
    private List<Order> toBeanList(List<Map<String,Object>> mapList) {
        List<Order> orderList = new ArrayList<>();
        for(Map<String,Object> map : mapList) {
           orderList.add(toBean(map));
        }
        return orderList;
    }
    private void loadOrderItem(Order order) throws SQLException {
        /*
         * 1. 给sql语句select * from t_orderitem where oid=?
         * 2. 执行之，得到List<OrderItem>
         * 3. 设置给Order对象
         */
        String sql = "select * from t_orderitem where oid=?";
        List<Map<String,Object>> mapList = tqr.query(sql, new MapListHandler(), order.getOid());
        List<Orderitem> orderItemList = toOrderItemList(mapList);

        order.setOrderitems(orderItemList);
    }

    /**
     * 把多个Map转换成多个OrderItem
     * @param mapList
     * @return
     */
    private List<Orderitem> toOrderItemList(List<Map<String, Object>> mapList) {
        List<Orderitem> orderItemList = new ArrayList<Orderitem>();
        for(Map<String,Object> map : mapList) {
            Orderitem orderItem = toOrderItem(map);
            orderItemList.add(orderItem);
        }
        return orderItemList;
    }

    /*
     * 把一个Map转换成一个OrderItem
     */
    private Orderitem toOrderItem(Map<String, Object> map) {
        Orderitem orderItem = BeanUtil.toBeans(map, Orderitem.class);
        Book book = BeanUtil.toBeans(map, Book.class);
        orderItem.setBook(book);
        return orderItem;
    }

}
